package com.aitaoxue.palx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

/**
 * 全局配置类
 * 
 * @author earthgee
 *
 */
public class AppConfig {
	protected static final String APP_CONFIG = "config";

	public static final String CONF_APP_UNIQUEID = "APP_UNIQUEID";

	public static final String CONF_LOAD_IMAGE = "pref_loadimage";

	public static final String CONF_RECEIVENOTICE = "pref_reveivenotice";

	public static final String CONF_VOICE = "pref_voice";

	public static final String CONF_CHECKUP = "pref_checkup";

	public static final String CONF_FIRST_START = "isFirstStart";

	/**
	 * 保存图片的路径
	 */
	public static final String DEFAULT_SAVE_IMAGE_PATH = Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "Palx"
			+ File.separator + "img" + File.separator;

	/**
	 * 保存文件的路径
	 */
	public static final String DEFAULT_SAVE_FILE_PATH = Environment
			.getExternalStorageDirectory()
			+ File.separator
			+ "Palx"
			+ File.separator + "download" + File.separator;

	private Context context;
	private static AppConfig appConfig;

	/**
	 * 获得AppConfig
	 * 
	 * @param context
	 * @return
	 */
	public static AppConfig getAppConfig(Context context) {
		if (appConfig == null) {
			appConfig = new AppConfig();
			appConfig.context = context;
		}
		return appConfig;
	}

	/**
	 * 获得应用默认的pref
	 * 
	 * @param context
	 * @return
	 */
	public static SharedPreferences getSharedPreferences(Context context) {
		return PreferenceManager.getDefaultSharedPreferences(context);
	}

	
	public static boolean isLoadImage(Context context) {
		return getSharedPreferences(context).getBoolean(CONF_LOAD_IMAGE, true);
	}

	/**
	 * 获得appconfig中键为key的值
	 * 
	 * @param key
	 * @return
	 */
	public String get(String key) {
		Properties props = get();
		return (props != null) ? props.getProperty(key) : null;
	}

	/**
	 * 获得appconfig文件
	 * 
	 * @return Properties
	 */
	public Properties get() {
		FileInputStream fis = null;
		Properties props = new Properties();
		try {
			File dirConf = context.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			fis = new FileInputStream(dirConf.getPath() + File.separator
					+ APP_CONFIG);
			props.load(fis);
		} catch (Exception e) {
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
			}
		}

		return props;
	}

	/**
	 * 保存属性在Properties
	 * 
	 * @param p
	 */
	public void setProps(Properties p) {
		FileOutputStream fos = null;
		try {
			File dirConf = context.getDir(APP_CONFIG, Context.MODE_PRIVATE);
			File conf = new File(dirConf, APP_CONFIG);
			fos = new FileOutputStream(conf);

			p.store(fos, null);
			fos.flush();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void set(Properties ps){
		Properties props=get();
		props.putAll(ps);
		setProps(props);
	}
	
	public void set(String key,String value){
		Properties props=get();
		props.put(key, value);
		setProps(props);
	}
	
	public void remove(String... key){
		Properties props=get();
		for(String str:key){
			props.remove(str);
		}
		setProps(props);
	}
}










